/*
Copyright 2005-2014 CP Fricano - All Rights Reserved

This software and source code is licensed under the Apache
License, Version 2.0. You may not use this software or source
code except in compliance with the License.  You may obtain a
copy of the License at: http://www.apache.org/licenses/LICENSE-2.0.
Use and distributed under this License is on an "as is" basis,
without expressed or implied warranties or conditions of any kind.
See the License for the specific language governing permissions and
limitations.
 */

package com.fricano.java42.swing.dings.tester;

import java.awt.Color;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JTree;
import javax.swing.border.LineBorder;

import com.fricano.java42.base.components.helper.Helper_Image;
import com.fricano.java42.lang.System;
import com.fricano.java42.sequencer.J42Helper;
import com.fricano.java42.sequencer.J42Listener;
import com.fricano.java42.sequencer.J42Sequencer;
import com.fricano.java42.sequencer.J42Sequencer.LookAndFeel;
import com.fricano.java42.swing.dings.J42SwingDingController_Slider.Alignment;
import com.fricano.java42.swing.dings.J42SwingDingManager_Recorder;
import com.fricano.java42.swing.dings.J42SwingDingManager_Recorder.RecorderCallback;
import com.fricano.java42.swing.dings.J42SwingDingManager_Recorder.RecorderSettings;
import com.fricano.java42.swing.dings.J42SwingDingManager_Slider;
import com.fricano.java42.swing.dings.J42SwingDing_Factory;
import com.fricano.java42.swing.dings.components.J42JPanel_Manageable;
import com.fricano.java42.swing.dings.components.J42JPanel_Manageable.Manager;

final public class J42SwingDing_ScreenRecorderTest_01 {

    private static JComponent                   slider_01 = null;
    private static JTree                        slider_02 = null;
    private static J42SwingDing_Factory         dingFactory;
    private static J42SwingDingManager_Slider   dingSlider_01;
    private static J42SwingDingManager_Slider   dingSlider_02;
    private static J42SwingDingManager_Recorder dingRecorder;
    private static Manager                      managerContentPane;

    // private static J42SwingDing_ClockOverlay j42SwingDing_ClockOverlay = null;

    static public void main(final String[] args) throws Exception {

        // CallerId.setEnable(true);
        // CallerId.setEnableEntryExit(true);
        // CallerId.setEnableInfo(true);

        final J42Sequencer<JFrame> j42Sequencer = new J42Sequencer<JFrame>(LookAndFeel.CROSSPLATFORM) {

            @Override
            public void doSequence0_CreateComponents(final JFrame window, final Container contentPane, final J42Helper<JFrame> helper) {

                // --------------------------------------------------------------
                // Replace the default content pane with a manageable pane
                // --------------------------------------------------------------
                final J42JPanel_Manageable panel = new J42JPanel_Manageable() {
                    {
                        managerContentPane = getManager();
                        getManager().addClockOverlay();
                        setBackground(new Color(171, 191, 255, 251));
                        final Color bg = getBackground();
                        final Color c1 = new Color(bg.getRed(), bg.getGreen(), bg.getBlue());
                        final Color c2 = new Color(22, 22, 22, 0);
                        getManager().setBackgroundGradientPaint(c1, c2);
                        // getManager().setForegroundGradientPaint(c1, c2);
                    }
                };
                window.setContentPane(panel);

                // --------------------------------
                // Create a SwingDing factory
                // --------------------------------
                dingFactory = new J42SwingDing_Factory(window);

                // --------------------------------
                // Create two slide managers
                // --------------------------------
                dingSlider_01 = dingFactory.create_SlideManager();
                dingSlider_02 = dingFactory.create_SlideManager();

                // --------------------------------
                // Create two sliders
                // --------------------------------
                slider_01 = new JButton("CaptureFrame");
                slider_02 = new JTree();
                slider_01.setBorder(new LineBorder(Color.RED, 2));
                slider_01.setSize(200, 50);
                slider_02.setBorder(new LineBorder(Color.RED, 2));
                slider_02.setSize(200, 150);

                // --------------------------------
                // Create the screen recorder
                // --------------------------------
                final RecorderCallback recorderCallback = createRecorderCallbackHandler();
                dingRecorder = dingFactory.create_ScreenRecorder(new RecorderSettings(), recorderCallback);

            }

            @Override
            public void doSequence1_AddComponents(final JFrame frame, final Container contentPane, final J42Helper<JFrame> helper) {

                final JButton b = new JButton("Slide");
                b.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(final ActionEvent e) {
                        dingSlider_01.slideToggle(true);
                        dingSlider_02.slideToggle(true);
                    }
                });
                contentPane.add(b);

                helper.addJButton(contentPane, "Record", new J42Listener<JButton, ActionEvent>() {

                    @Override
                    public void doWorkOutsideEDT(final JButton t, final ActionEvent eventObject) throws Exception {
                        dingRecorder.startInteractiveMultiFrameRecorderGUI(null);
                    }

                    @Override
                    public void doWorkInsideEDT(final JButton t, final ActionEvent eventObject) throws Exception {
                    }

                });

            }

            @Override
            public void doSequence2_LocateSizePackShow(final JFrame window, final Container contentPane, final J42Helper<JFrame> helper) {
                window.setLocationRelativeTo(null);
                window.setVisible(true);
            }

            @Override
            public void doSequence3_StartApplication(final JFrame window, final Container contentPane, final J42Helper<JFrame> helper) {

                // --------------------------------------------------------------
                // Replace the System class with the J42 System class
                // --------------------------------------------------------------
                if (true) {
                    System.getManager().initialize(window);
                }

                // Helper_Native.enableSystemMenu(window, false);
                // j42SwingDing_ClockOverlay = new J42SwingDing_ClockOverlay(60);
                // j42SwingDing_ClockOverlay.attach(window.getContentPane());
                // j42SwingDing_ClockOverlay.start();

                dingSlider_01.attachSlider(Alignment.EXTERIOR_LEFT, slider_01, 42, 600, 0, 0);
                dingSlider_01.slideToggle(true);
                dingSlider_02.attachSlider(Alignment.INTERIOR_BOTTOM, slider_02, 42, 1000, 0, 0);

            }

            @Override
            public void doSequence9_StopApplication(final JFrame window, final Container contentPane, final J42Helper<JFrame> helper) {
                window.dispose();
                managerContentPane.getClockOverlay().shutdown();
            }
        };

    }

    private static RecorderCallback createRecorderCallbackHandler() {

        return new RecorderCallback() {

            @Override
            public void started() {
                managerContentPane.getClockOverlay().reset();
            }

            @Override
            public final boolean imageFilter(final BufferedImage bufferedImage) {
                managerContentPane.getClockOverlay().addWaterMark(bufferedImage, 60);
                Helper_Image.addWaterMark(bufferedImage, "Java42 Recorder", 10);
                return true;
            }

            @Override
            public void stopped(final RecorderSettings recorderSettings, final ImageIcon imageIcon) {
                final File file = new File("anamation.gif");
                Helper_Image.saveAnimatedGif(recorderSettings.gifImageInfo, file);
                final long fileSize = file.length();
                System.out.println(imageIcon);
                System.out.println(file.getName() + " (" + fileSize + " bytes)");
            }
        };
    }
}
